'''
Created on 2013-6-22

@author: ZuoPeng
'''

import functools
class P74(object):
    '''
    classdocs
    '''
  
    def __init__(self):
        '''
        Constructor
        '''
        self.l = {}
        for n in range(10):
            self.l[n] = self.Factorial(n)
        self.r = {}
        for n in range(1, 10000000):
            self.r[n] = -1
                
    def Factorial(self, n):
        if n == 0:
            return 1    
        return functools.reduce(lambda x, y:x * y, range(1, n + 1))
    
    def SumOfFactorial(self, n):
        if n < 10:
            return self.l[n]
        else:
            return self.l[n % 10] + self.SumOfFactorial(n // 10)

    def Chain(self, n):
        if self.r[n] == -1:
            l=[]
            k=n
            while not k in l:
                l.append(k)
                k=self.SumOfFactorial(k)
            self.r[n]=len(l)
            
            x=l.pop()
            while x!=k:
                try:
                    self.r[x]=self.r[n]-l.index(k)
                except Exception:
                    print (x)
                    raise
                x=l.pop()
        return self.r[n]

        
    def calc(self):
        count = 0
        for n in range(1, 1000000):
            if self.Chain(n) == 60:
                count += 1
                print(str(n) + ' ' + str(count))

        
       
        
p = P74()
p.calc()
